<!DOCTYPE html>
<html lang="zh-CN"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorate="~{layout/default.html}">
<head>
    <title>表信息</title>
</head>
<body>
<section layout:fragment="wrapper">
    <form class="layui-form" th:object="${entity}" action="">
        <div class="mainBox">
            <div class="main-container">
                <div class="layui-card">
                    <div class="layui-card-body">
                        <input type="hidden" name="id" th:value="*{id}">
                        <div class="layui-row">
                            <div class="layui-col-md6 layui-col-sm6">
                                <div class="layui-form-item">
                                    <label class="layui-form-label">数据源</label>
                                    <div class="layui-input-block">
                                        <input type="hidden" name="sourceId" th:value="*{sourceId}">
                                        <input type="text" name="sourceName" class="layui-input"
                                               th:value="*{sourceName}" readonly>

                                    </div>
                                </div>
                            </div>
                            <div class="layui-col-md6 layui-col-sm6">
                                <div class="layui-form-item">
                                    <label class="layui-form-label">表名</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="tableName" class="layui-input"
                                               th:value="*{tableName}" readonly>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-row">
                            <div class="layui-col-md6 layui-col-sm6">
                                <div class="layui-form-item">
                                    <label class="layui-form-label">名称</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="name" lay-verify="required" autocomplete="off"
                                               maxlength="50" placeholder="请输入名称" class="layui-input"
                                               th:value="*{name}">
                                    </div>
                                </div>
                            </div>
                            <div class="layui-col-md6 layui-col-sm6">
                                <div class="layui-form-item">
                                    <label class="layui-form-label">表说明</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="tableComment" lay-verify="required" autocomplete="off"
                                               maxlength="50" placeholder="请输入说明" class="layui-input"
                                               th:value="*{tableComment}">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-row">
                            <div class="layui-col-md6 layui-col-sm6">
                                <div class="layui-form-item">
                                    <label class="layui-form-label">实体</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="entityName" lay-verify="required" autocomplete="off"
                                               maxlength="100" placeholder="请输入实体" class="layui-input"
                                               th:value="*{entityName}">
                                    </div>
                                </div>
                            </div>
                            <div class="layui-col-md6 layui-col-sm6">
                                <div class="layui-form-item">
                                    <label class="layui-form-label">包名</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="packageName" lay-verify="required" autocomplete="off"
                                               maxlength="100" placeholder="请输入包名" class="layui-input"
                                               th:value="*{packageName}">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-row">
                            <div class="layui-col-md6 layui-col-sm6">
                                <div class="layui-form-item">
                                    <label class="layui-form-label">xml路径</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="xmlPath" lay-verify="required" autocomplete="off"
                                               maxlength="200" placeholder="请输入xml路径" class="layui-input"
                                               th:value="*{xmlPath}">
                                    </div>
                                </div>
                            </div>
                            <div class="layui-col-md6 layui-col-sm6">
                                <div class="layui-form-item">
                                    <label class="layui-form-label">html路径</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="htmlPath" lay-verify="required" autocomplete="off"
                                               maxlength="200" placeholder="请输入html路径" class="layui-input"
                                               th:value="*{htmlPath}">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-row">
                            <div class="layui-col-md6 layui-col-sm6">
                                <div class="layui-form-item">
                                    <label class="layui-form-label">js路径</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="jsPath" lay-verify="required" autocomplete="off"
                                               maxlength="200" placeholder="请输入js路径" class="layui-input"
                                               th:value="*{jsPath}">
                                    </div>
                                </div>
                            </div>
                            <div class="layui-col-md6 layui-col-sm6">
                                <div class="layui-form-item">
                                    <label class="layui-form-label">css路径</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="cssPath" lay-verify="required" autocomplete="off"
                                               maxlength="200" placeholder="请输入css路径" class="layui-input"
                                               th:value="*{cssPath}">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-row">
                            <div class="layui-col-md6 layui-col-sm6">
                                <div class="layui-form-item">
                                    <label class="layui-form-label">sql路径</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="sqlPath" lay-verify="required" autocomplete="off"
                                               maxlength="200" placeholder="请输入sql路径" class="layui-input"
                                               th:value="*{sqlPath}">
                                    </div>
                                </div>
                            </div>
                            <div class="layui-col-md6 layui-col-sm6">
                                <div class="layui-form-item">
                                    <label class="layui-form-label">排序</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="sort" lay-verify="required|number" autocomplete="off"
                                               maxlength="10" placeholder="请输入排序" class="layui-input"
                                               th:value="*{sort} ?: 1" id="sort">
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-card">
                <div class="layui-card-header">
                    表字段
                </div>
                <div class="layui-card-body">
                    <table class="layui-table">
                        <thead>
                        <tr>
                            <td style="width: 80px;">序号</td>
                            <td>字段名</td>
                            <td>属性名</td>
                            <td>说明</td>
                            <td>类型</td>
                            <td>类型名称</td>
                            <td>主键</td>
                            <td>基类字段</td>
                            <td>列表项</td>
                            <td>查询项</td>
                            <td>排序</td>
                            <td>表单项</td>
                            <td>必填</td>
                        </tr>
                        </thead>
                        <tbody>
                        <tr th:each="field,stat : *{genTableFields}">
                            <td>
                                <input type="hidden" th:name="'genTableFields[' + ${stat.index} +'].id'"
                                       th:value="${field.id}">
                                <input type="hidden" th:name="'genTableFields[' + ${stat.index} +'].tableId'"
                                       th:value="${field.tableId}">

                                <input type="text" th:name="'genTableFields[' + ${stat.index} +'].sort'"
                                       lay-verify="required|number" autocomplete="off"
                                       maxlength="10" placeholder="序号" class="layui-input"
                                       th:value="${field.sort} ?: ${stat.index + 1}">
                            </td>
                            <td>
                                <input type="text" th:name="'genTableFields[' + ${stat.index} +'].fieldName'"
                                       placeholder="字段名" class="layui-input" readonly
                                       th:value="${field.fieldName}">
                            </td>
                            <td>
                                <input type="text" th:name="'genTableFields[' + ${stat.index} +'].attrName'"
                                       lay-verify="required" autocomplete="off"
                                       maxlength="50" placeholder="属性名" class="layui-input"
                                       th:value="${field.attrName}">
                            </td>
                            <td>
                                <input type="text" th:name="'genTableFields[' + ${stat.index} +'].fieldComment'"
                                       lay-verify="required" autocomplete="off"
                                       maxlength="50" placeholder="字段说明" class="layui-input"
                                       th:value="${field.fieldComment}">
                            </td>
                            <td>
                                <input type="text" th:name="'genTableFields[' + ${stat.index} +'].attrType'"
                                       lay-verify="required" autocomplete="off"
                                       maxlength="50" placeholder="字段类型" class="layui-input"
                                       th:value="${field.attrType}">
                            </td>
                            <td>
                                <input type="text" th:name="'genTableFields[' + ${stat.index} +'].attrTypeName'"
                                       lay-verify="required" autocomplete="off"
                                       maxlength="100" placeholder="字段类型" class="layui-input"
                                       th:value="${field.attrTypeName}">
                            </td>
                            <td>
                                <input type="checkbox" th:name="'genTableFields[' + ${stat.index} +'].primaryPk'"
                                       class="layui-input" th:value="1" th:checked="${field.primaryPk eq '1'}"
                                       lay-skin="switch" lay-text="是|否">
                            </td>
                            <td>
                                <input type="checkbox" th:name="'genTableFields[' + ${stat.index} +'].baseField'"
                                       class="layui-input" th:value="1" th:checked="${field.baseField eq '1'}"
                                       lay-skin="switch" lay-text="是|否">
                            </td>
                            <td>
                                <input type="checkbox" th:name="'genTableFields[' + ${stat.index} +'].gridItem'"
                                       class="layui-input" th:value="1" th:checked="${field.gridItem eq '1'}"
                                       lay-skin="switch" lay-text="是|否">
                            </td>
                            <td>
                                <input type="checkbox" th:name="'genTableFields[' + ${stat.index} +'].queryItem'"
                                       class="layui-input" th:value="1" th:checked="${field.gridItem eq '1'}"
                                       lay-skin="switch" lay-text="是|否">
                            </td>
                            <td>
                                <input type="checkbox" th:name="'genTableFields[' + ${stat.index} +'].gridSort'"
                                       class="layui-input" th:value="1" th:checked="${field.gridSort eq '1'}"
                                       lay-skin="switch" lay-text="是|否">
                            </td>
                            <td>
                                <input type="checkbox" th:name="'genTableFields[' + ${stat.index} +'].formItem'"
                                       class="layui-input" th:value="1" th:checked="${field.formItem eq '1'}"
                                       lay-skin="switch" lay-text="是|否">
                            </td>
                            <td>
                                <input type="checkbox" th:name="'genTableFields[' + ${stat.index} +'].formRequired'"
                                       class="layui-input" th:value="1" th:checked="${field.formRequired eq '1'}"
                                       lay-skin="switch" lay-text="是|否">
                            </td>
                        </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <div class="bottom">
            <div class="button-container">
                <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit
                        lay-filter="form-save">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
                <button type="button" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit
                        lay-filter="form-code">
                    <i class="layui-icon layui-icon-template-1"></i>
                    生成代码
                </button>
                <button type="reset" class="pear-btn pear-btn-sm">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
                <button type="button" class="pear-btn pear-btn-sm"
                        lay-filter="close">
                    <i class="layui-icon layui-icon-close"></i>
                    关闭
                </button>
            </div>
        </div>
    </form>
    <script th:inline="javascript">
        layui.use(['form', 'jquery', 'button', 'util', 'numberInput'], function () {
            let form = layui.form;
            let button = layui.button;
            let $ = layui.jquery;
            let util = layui.util;
            let numberInput = layui.numberInput;

            numberInput.render("#sort", {
                min: 0,
                step: 1,
            });

            form.on('submit(form-save)', function (data) {
                // 禁用提交和重置按钮
                let $button = $('button[lay-filter="form-code"],button[type="reset"]');
                $button.addClass('layui-btn-disabled');
                $button.attr('disabled', 'disabled');

                // 防止重复提交
                button.load({
                    elem: '[lay-filter="form-save"]',
                    time: 2000,
                    done: function () {
                        $button.removeClass('layui-btn-disabled');
                        $button.removeAttr('disabled');
                    }
                })

                $.ajax({
                    url: '[(@{/genTableInfo/save})]',
                    data: data.field,
                    dataType: 'json',
                    contentType: 'application/x-www-form-urlencoded',
                    type: 'post',
                    success: function (result) {
                        if (result.code === '20000') {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                parent.layer.close(parent.layer.getFrameIndex(window.name)); //关闭当前页
                                parent.layui.table.reload("tb-table");
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
                return false;
            });

            form.on('submit(form-code)', function (data) {
                // 禁用提交和重置按钮
                let $button = $('button[lay-filter="form-save"],button[type="reset"]');
                $button.addClass('layui-btn-disabled');
                $button.attr('disabled', 'disabled');

                // 防止重复提交
                button.load({
                    elem: '[lay-filter="form-code"]',
                    time: 2000,
                    done: function () {
                        $button.removeClass('layui-btn-disabled');
                        $button.removeAttr('disabled');
                    }
                })

                $.ajax({
                    url: '[(@{/genTableInfo/code})]',
                    data: data.field,
                    dataType: 'json',
                    contentType: 'application/x-www-form-urlencoded',
                    type: 'post',
                    success: function (result) {
                        if (result.code === '20000') {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                parent.layer.close(parent.layer.getFrameIndex(window.name)); //关闭当前页
                                parent.layui.table.reload("tb-table");
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
                return false;
            })

            /**
             * 关闭弹窗
             */
            util.event('lay-filter', {
                close: function () {
                    parent.layer.close(parent.layer.getFrameIndex(window.name)); //关闭当前页
                    return false;
                }
            })
        })
    </script>
</section>
</body>
</html>
